<template>
  <div class="outflow_view">
    <!-- 添加或修改申请采购明细对话框 -->
    <el-dialog :visible.sync="open" :title="title" width="800px" append-to-body>
      <el-form ref="form" :model="form" label-width="190px" :rules="rules" style="margin-top: -20px" size="mini">
        <el-row>
          <el-col :span="12">
              <el-form-item label="品名:" label-width="130px">
                {{form.materialName}}
              </el-form-item>
          </el-col>
          <el-col :span="12">
              <el-form-item label="领用数量:" label-width="130px">
                {{form.occupationQuantity}}
              </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
              <el-form-item label="学院:" label-width="130px">
                {{getCollegeName}}
              </el-form-item>
          </el-col>
          <el-col :span="12">
              <el-form-item label="实验室:" label-width="130px">
                {{getLabName}}
              </el-form-item>
          </el-col>
        </el-row>
        
        <el-row>
          <el-col :span="24">
              <el-form-item label="领取时间:" label-width="130px">
                {{form.createTime}}
              </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
              <el-form-item label="领取人学(工)号:" prop="receiver1No" label-width="130px">
                <el-input v-model="form.receiver1No" @blur="searchUser1"
                  @keyup.enter.native="searchUser1" placeholder="请输入领取人学(工)号"/>
              </el-form-item>
          </el-col>
          <el-col :span="12">
              <el-form-item label="领取人姓名:" label-width="130px">
                <el-input v-model="form.receiver1Name" disabled placeholder="自动带入"/>
              </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
              <el-form-item label="领取人学(工)号:" prop="receiver2No" label-width="130px">
                <el-input v-model="form.receiver2No" @blur="searchUser2"
                  @keyup.enter.native="searchUser2" placeholder="请输入领取人学(工)号"/>
              </el-form-item>
          </el-col>
          <el-col :span="12">
              <el-form-item label="领取人姓名:" label-width="130px">
                <el-input v-model="form.receiver2Name" disabled placeholder="自动带入"/>
              </el-form-item>
          </el-col>
        </el-row>
        <hr>
        <el-row>
          <el-col :span="12">
              <el-form-item label="包装上二维码:" label-width="130px">
                <el-input v-model="qrCode" placeholder="请输入二维码"/>
              </el-form-item>
          </el-col>
          <el-col :span="12">
              <div style="float:right;" class="purchase_s_view">
                <el-button type="primary" @click="addQcCode">确 定</el-button>
              </div>
          </el-col>
      </el-row>
      </el-form>
      <el-table style="width: 100%; margin-bottom: 20px; margin-top: -15px" :data="deliverList">
          <el-table-column label="序号" align="center" type="index"/>
          <el-table-column label="二维码内容" align="left" prop="qrCode" :show-overflow-tooltip="true" />
          <el-table-column label="出库人" align="center" prop="createBy" />
          <el-table-column label="出库时间" align="center" prop="createTime" />
          <el-table-column label="操作" align="right" fixed="right">
            <template slot-scope="scope">
              <div class="purchase_s_view"> 
                <el-button
                  size="mini"
                  class="active"
                  type="text"
                  style="margin-left: 5px; margin-right: 10px"
                  @click="deleteDeliver(scope.row)"
                >移除</el-button>
              </div>
            </template>
          </el-table-column>
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" v-hasPermi="['system:deliver:edit']">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { searchEmployee } from "@/api/labdata/employee"
import { formatCurrentDate } from "@/utils/index"
import { getDetails, deliver } from "@/api/labdanger/occupationDetail"
import { getOrgByFunctionOpt, listSecondOrgLab } from "@/api/labdata/orgnization"
export default {
  name: "outflowReceive",
  data() {
    return {
      title:'出库确认',
      open: false,
      // 表单参数
      form: {
        receiver1No:null,
        receiver1Name:null,
        receiver2No:null,
        receiver2Name:null,
      },
      //二级机构
      secondOrgList: [],
      //二级机构下的实验室
      secondOrgLabList: [],
      qrCode: '',
      deliverList: [],
      rules: {
        receiver1No: {required: true, message: '领取人学(工)号', trigger: 'blur'},
        receiver1Name: {required: true, message: '领取人姓名', trigger: 'blur'},
        receiver2No: {required: true, message: '领取人学(工)号', trigger: 'blur'},
        receiver2Name: {required: true, message: '领取人姓名', trigger: 'blur'}
      }
    };
  },
  props: {
  },
  created() {
    this.getCollege()
  },
  computed: {
    getCollegeName() {
      const org = this.secondOrgList.find(item => item.orgId == this.form.collegeId)
      return org ? org.orgName : ''
    },
    getLabName() {
      const org = this.secondOrgLabList.find(item => item.orgId == this.form.labId)
      return org ? org.orgName : ''
    }
  },
  updated() {
  },
  methods: {
    searchUser1() {
      if (!this.form.receiver1No) {
        this.form.receiver1No = null;
        return;
      }
      searchEmployee(this.form.receiver1No).then(response => {
        let searchResult = response.data;
        if (searchResult) {
          this.form.receiver1No = searchResult.employeeNo
          this.form.receiver1Name = searchResult.nickName
        } else {
          this.form.receiver1No = null
          this.form.receiver1Name = null
        }
        //强制刷新
        this.$forceUpdate();
      });
    },
    searchUser2() {
      if (!this.form.receiver2No) {
        this.form.receiver2No = null;
        return;
      }
      searchEmployee(this.form.receiver2No).then(response => {
        let searchResult = response.data;
        if (searchResult) {
          this.form.receiver2No = searchResult.employeeNo
          this.form.receiver2Name = searchResult.nickName
        } else {
          this.form.receiver2No = null
          this.form.receiver2Name = null
        }
        //强制刷新
        this.$forceUpdate();
      });
    },
    deleteDeliver(row) {
      const index = this.deliverList.findIndex(item => item.qrCode == row.qrCode)
      this.deliverList.splice(index, 1)
    },
    addQcCode() {
      if (!this.qrCode) {
        this.msgError("请先输入二维码");
        return
      }
      const row = this.deliverList.find(item => item.qrCode == this.qrCode)
      if (row) {
        this.msgError("该二维码已添加")
        this.qrCode = ''
        return
      }
      this.deliverList.push({
        qrCode: this.qrCode,
        createTime: formatCurrentDate(),
        createBy: this.$store.state.user.name
      })
      this.qrCode = ''
    },
    searchDetails(detailId) {
      getDetails(detailId).then(response => {
        this.form = response.data;
        this.getLabOrgs()
        //强制刷新
        this.$forceUpdate();
      })
    },
    getCollege() {
      getOrgByFunctionOpt(2).then(response => {
        this.secondOrgList = response.data;
      });
    },
    getLabOrgs() {
      listSecondOrgLab(this.form.collegeId).then(response => {
        this.secondOrgLabList = response.data;
      });
    },
    /** 新增按钮操作 */
    handleShow(row) {
      this.reset(); 
      this.searchDetails(row.occupationDetailsId)
      this.open = true;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
      };
      this.qrCode = null
      this.deliverList = []
    },
    submitForm() {
      this.$refs['form'].validate((valid)=> {
        if (valid) {
          if (this.deliverList.length != this.form.occupationQuantity) {
            this.msgError("出库数量不正确");
            return
          }
          this.form.deliverList = this.deliverList
          deliver(this.form).then(response => {
            this.open = false;
            this.$emit("list");
          })
        }
      })
    }
  }
};
</script>

<style scoped>
    .el-button.active{
    text-decoration: underline;
    color: #0000ee;
    margin-left: 15px;
  }
  .dialog-footer {
      display: flex;
      flex-direction: row;
      align-items: center;
      justify-content: center;
  }
</style>